package com.jdb.settings.controller.assembler;

import com.alibaba.fastjson.JSON;
import com.jdb.settings.api.SettingDTO;
import com.jdb.settings.privoder.entity.SettingDO;
import org.springframework.cglib.beans.BeanCopier;

import java.util.Objects;

public class SettingAssembler {
    private static final BeanCopier fromCopier = BeanCopier.create(
            SettingDTO.class, SettingDO.class, false);
    private static final BeanCopier toCopier = BeanCopier.create(
            SettingDO.class, SettingDTO.class, false);

    public static SettingDO fromDTO(SettingDTO settingDTO) {
        if (Objects.isNull(settingDTO)) {
            return null;
        }
        SettingDO loanDO = new SettingDO();
        fromCopier.copy(settingDTO, loanDO, null);
        loanDO.setOptions(JSON.toJSONString(settingDTO.getOptions()));
        return loanDO;
    }

    public static SettingDTO toDTO(SettingDO settingDO) {
        if (Objects.isNull(settingDO)) {
            return null;
        }
        SettingDTO loanDTO = new SettingDTO();
        toCopier.copy(settingDO, loanDTO, null);
        loanDTO.setOptions(JSON.parseArray(settingDO.getOptions()));
        return loanDTO;
    }
}
